import path from 'path'
import { UnifiedWebpackPluginV5 } from 'weapp-tailwindcss/webpack'


const config = {
  projectName: 'soga',
  date: '2023-8-16',
  designWidth(input) {
    // 配置 NutUI 375 尺寸
    if (input?.file?.replace(/\\+/g, '/').indexOf('@nutui') > -1) {
      return 375
    }
    // 全局使用 Taro 默认的 750 尺寸
    return 750
  },
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  sourceRoot: 'src',
  outputRoot: `dist/${process.env.TARO_ENV}`,
  plugins: [
    '@tarojs/plugin-html',
    '@tarojs/plugin-http',
    '@tarojs/plugin-mock',
    
  ],
  defineConstants: {
  },
  copy: {
    patterns: [
    ],
    options: {
    }
  },
  framework: 'react',
  compiler: { type: 'webpack5', prebundle: { enable: false } },
  cache: {
    enable: false // Webpack 持久化缓存配置，建议开启。默认配置请参考：https://docs.taro.zone/docs/config-detail#cache
  },
  mini: {
    navigationStyle: 'custom', // 自定义导航栏
    debugReact: true,
    optimizeMainPackage: {
      enable: true
    },
    postcss: {
      pxtransform: {
        enable: true,
        config: {

        }
      },
      url: {
        enable: true,
        config: {
          limit: 1024 // 设定转换尺寸上限
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    },
    rules: [
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: 'static/img/[name].[hash:7].[ext]'
            }
          }
        ]
      },
    ],
    webpackChain(chain, webpack) {
      chain.merge({
        plugin: {
          install: {
            plugin: UnifiedWebpackPluginV5,
            args: [{
              appType: 'taro'
            }]
          }
        }
      })
    },
  },
  h5: {
    webpackChain (chain, webpack) {
      chain.merge({
        module: {
          rule: {
            myloader: {
              test: /\.js$/, 
              use: [{
                loader: 'babel-loader',
                options: {}
              }]
            }
          }
        }
      })
    },
    publicPath: '/',
    staticDirectory: 'static',
    esnextModules: ['taro-ui', 'taro_rich_text'],
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    },
  },
  alias: {
    '@/api': path.resolve(__dirname, '..', 'src/shared/api'),
    '@/ui': path.resolve(__dirname, '..', 'src/shared/ui'),
    '@/model': path.resolve(__dirname, '..', 'src/shared/model'),
    '@/fp': path.resolve(__dirname, '..', 'src/shared/fp'),
    '@/user': path.resolve(__dirname, '..', 'src/user'),
    '@/shared': path.resolve(__dirname, '..', 'src/shared'),
    '@/theme': path.resolve(__dirname, '..', 'src/theme'),
    
  },
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}
